<?php
namespace Home\Controller;
use Think\Controller;
use Think\Image;
use Home\Model\UserModel;



class UserController extends CommonController {
     //保存微信openid  
    private $weixin_openid = ""; 
    //保存从微信得到的code   
    private $code="";
    //获得菜单中设置的state值  
    private $state="";
    //默认进行的函数 获取到用户的openid并且存到session里面
    public function _initialize(){
      
        if (isset($_GET['code']) && isset($_GET['state']) ){  
        $this->weixin_openid = GetOpenid($_GET['code']);  
        $this->code = $_GET['code'];  
        $this->state = $_GET['state'];  
        //echo $resultStr;  
        }else{  
            return ;  
        }  
        //把openid存入session
        //$_SESSION['weixin_openid']=$this->weixin_openid;  
        session("weixin_openid",$this->weixin_openid);
    }



	//显示用户账户页面，并且将收藏个数等信息显示出来
    public function user(){
    	//获取用户有多少条动态
    	//先写死，明天从session中获取
        $openid=session("weixin_openid");
    	//new一个user模型
    	$user = new UserModel('user','kl_');
    	//获取用户动态的数量
    	$dynamicNum = $user->getDynamicNum($openid);
    	//取出用户用过的优惠
    	$prefeNum = $user->getPrefeNum($openid);
    	//收藏的商家数量
    	$shopsNum = $user->getShopsNum($openid);
    	//获取其他人动态数量
    	$otherNum = $user->getOtherNum($openid);
    	$num = array(
    		'dynamic' => $dynamicNum, 
    		'prefeNum'=> $prefeNum,
    		'shopsNum'=> $shopsNum,
    		'otherNum'=> $otherNum
    		);
        $userdata = $this->getinfobyopenid(session("weixin_openid"));
        $this->assign("user",$userdata);
    	$this->assign("num",$num);
     
        $this->display("user");
    }

}